package tech.abing.spider.task.pipeline;

import java.util.List;

import org.apache.log4j.Logger;

import tech.abing.spider.component.Pipeline;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.task.model.ProductID;
import tech.abing.spider.util.ProccessableUtil;
import tech.abing.spider.util.RedisUtil;
import tech.abing.spider.util.SysUtil;

public class ProductIDPipeline implements Pipeline {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(ProductIDPipeline.class);
	
	private static RedisUtil redisUtil = null;
	
	public ProductIDPipeline() {
		
		if(redisUtil == null){
			redisUtil = RedisUtil.getInstance();
			logger.info("Redis连接池创建成功！");
		}
	}
	
	
	@Override
	public void proccessData(List<Proccessable> objectContainer) {

		if (ProccessableUtil.instanceOfClass(objectContainer, ProductID.class)) {// 判断集合类型是否是ProductID
			List<ProductID> list = ProccessableUtil.convert(objectContainer, ProductID.class);// 转换
			String redisTask = SysUtil.getValue("redis.task_product_id");
			for (ProductID prod : list) {
				
				//push到制定队列中
				try {
					redisUtil.lpush(prod, redisTask);
					logger.info("产品ID["+prod+"]入队队尾["+redisTask+"]成功");
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("产品ID["+prod+"]入队队尾["+redisTask+"]失败", e);
				}
				
			}
		}

	}

	@Override
	public void destory() {
		
		if(redisUtil != null){
			redisUtil.destroy();
			logger.info("Redis连接池关闭成功！");
		}
	}

}
